const mysql = require('mysql2/promise');

// 数据库配置
const dbConfig = {
  host: '43.143.253.188',
  user: 'nextjs',
  password: 'yEAiRE2jcxTyRBy5',
  database: 'nextjs',
  charset: 'utf8mb4'
};

async function checkCurrentMapAreas () {
  const connection = await mysql.createConnection(dbConfig);

  try {
    console.log('🔍 检查当前数据库中地图区域状态...\n');

    // 1. 检查顶级区域（parent_id为NULL或特定值）
    console.log('📋 检查顶级区域:');
    const [topLevelAreas] = await connection.execute(`
            SELECT id, name, parent_id, extension 
            FROM map_areas 
            WHERE parent_id IN (100, 19, 43) OR parent_id IS NULL
            ORDER BY id
        `);

    console.log('当前顶级区域:');
    topLevelAreas.forEach(area => {
      const parentInfo = area.parent_id ? `(父级: ${area.parent_id})` : '(顶级)';
      const extensionInfo = area.extension ? ` ${area.extension}` : '';
      console.log(`  ID: ${area.id.toString().padStart(4)} | ${area.name}${extensionInfo} | ${parentInfo}`);
    });

    // 2. 检查标准数据中需要的顶级区域ID
    console.log('\n📋 标准数据中需要的顶级区域ID:');
    const standardTopLevelIds = [100, 19, 43];
    const standardTopLevelNames = ['魏国', '蜀国', '吴国'];

    standardTopLevelIds.forEach((id, index) => {
      console.log(`  ID: ${id} | ${standardTopLevelNames[index]}`);
    });

    // 3. 检查这些ID是否被占用
    console.log('\n📋 检查标准顶级区域ID占用情况:');
    for (let i = 0; i < standardTopLevelIds.length; i++) {
      const id = standardTopLevelIds[i];
      const name = standardTopLevelNames[i];

      const [occupied] = await connection.execute(
        'SELECT id, name, parent_id, extension FROM map_areas WHERE id = ?',
        [id]
      );

      if (occupied.length > 0) {
        const area = occupied[0];
        console.log(`  ❌ ID ${id} (${name}) 被占用: ${area.name} (父级: ${area.parent_id || 'NULL'})`);
      } else {
        console.log(`  ✅ ID ${id} (${name}) 可用`);
      }
    }

    // 4. 检查当前有子区域的区域
    console.log('\n📋 检查当前有子区域的区域:');
    const [areasWithChildren] = await connection.execute(`
            SELECT DISTINCT parent_id, COUNT(*) as child_count
            FROM map_areas 
            WHERE parent_id IS NOT NULL 
            GROUP BY parent_id 
            ORDER BY parent_id
        `);

    areasWithChildren.forEach(area => {
      console.log(`  父级ID: ${area.parent_id} | 子区域数量: ${area.child_count}`);
    });

    // 5. 统计信息
    console.log('\n📊 数据库统计信息:');
    const [totalCount] = await connection.execute('SELECT COUNT(*) as total FROM map_areas');
    const [parentCount] = await connection.execute('SELECT COUNT(*) as total FROM map_areas WHERE parent_id IS NOT NULL');
    const [extensionCount] = await connection.execute('SELECT COUNT(*) as total FROM map_areas WHERE extension IS NOT NULL AND extension != ""');

    console.log(`  总区域数: ${totalCount[0].total}`);
    console.log(`  有父级区域数: ${parentCount[0].total}`);
    console.log(`  有等级信息区域数: ${extensionCount[0].total}`);

  } catch (error) {
    console.error('❌ 检查失败:', error);
  } finally {
    await connection.end();
  }
}

// 运行脚本
if (require.main === module) {
  checkCurrentMapAreas().catch(console.error);
}

module.exports = { checkCurrentMapAreas }; 